// pages/index/acrossAd/acrossAd.js
import {
        list as wxPageList
} from '../../../../api/wxPage'
const util = require('../../../../utils/util')
import {
        list as navigationList
} from '../../../../api/navigation'
let app = getApp();
Component({
        /**
         * 组件的属性列表
         */
        properties: {
                info: {
                        type: Object,
                }
        },
        lifetimes: {
                attached() {
                        if (this.data.info.tagids != null) {
                                this.data.tagIds = this.data.info.tagids.split(',')
                                for (var i = 0; i < this.data.tagIds.length; i++) {
                                        this.data.tagIds[i] = parseInt(this.data.tagIds[i])
                                }
                        }
                        this.setData({
                                tagIds: this.data.tagIds
                        })

                        this.loadPage()
                        this.loadnavigation()
                }
        },
        /**
         * 组件的初始数据
         */
        data: {

                navigations: [],
                pageList: [],
                tagIds: [],
                haveThumbnial: false
        },

        created() {


        },
        ready: function(e) {

        },

        /**
         * 组件的方法列表
         */

        methods: {
                loadnavigation() {
                        if (this.data.tagIds.length > 0) {
                                navigationList({
                                        mchId: app.globalData.mchId,
                                        tagIds: this.data.tagIds
                                }).then(res => {
                                        res.data.data.forEach(item=>{
                                                item.logo=util.thumbnail(item.logo,430,196)
                                        })
                                        this.data.navigations = res.data.data
                                        this.setData({
                                                navigations: this.data.navigations
                                        })
                                })
                        } else {
                                navigationList({
                                        mchId: app.globalData.mchId,
                                }).then(res => {
                                        this.data.navigations = res.data.data
                                        this.setData({
                                                navigations: this.data.navigations
                                        })
                                })
                        }
                },
                topage(e) {

                        wx.navigateTo({
                                url: '/pages/index/wxPage/wxPage?id=' + e.currentTarget.dataset.id
                        })

                },
                toLink(e) {
                        console.log(e)
                        if (e.currentTarget.dataset.id.type == 1) {
                                wx.navigateTo({
                                        url: '/pages/goods/category/category?id=' + e.currentTarget.dataset.id.typeId
                                })
                        }
                        if (e.currentTarget.dataset.id.type == 2) {
                                wx.navigateTo({
                                  url: '/pages/index/wxPage/wxPage?id=' + e.currentTarget.dataset.id.typeId
                                })
                              }
                        if (e.currentTarget.dataset.id.type == 3) {
                                wx.navigateTo({
                                        url: '/pages/goods/product/details?id=' + e.currentTarget.dataset.id.typeId,
                                })
                        }
                },
                loadPage() {
                        var _this = this;
                        wxPageList({
                                mchId: app.globalData.mchId,
                                tagIds: this.data.tagIds,
                        }).then(res => {
                                _this.data.pageList = res.data.data
                                 let haveThumbnial = this.data.haveThumbnial
                                 if (_this.data.pageList.length > 0) {
                                if (!util.isNull(_this.data.pageList[0].thumbnial)) {
                                        _this.data.pageList[0].thumbnial = util.thumbnail(_this.data.pageList[0].thumbnial, 710, 446)
                                        haveThumbnial = true
                                }
                        }
                                _this.setData({
                                        pageList: _this.data.pageList,
                                        haveThumbnial: haveThumbnial
                                })
                        })
                }
        }
})